package ldh.im.rpc.connection.socket;

import ldh.im.rpc.config.RpcConfig;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class RpcServerSocket implements Runnable {

    private RpcConfig rpcConfig;

    private ServerSocket serverSocket;
    private volatile boolean isStop = false;

    public RpcServerSocket(RpcConfig rpcConfig, int port) throws IOException {
        this.rpcConfig = rpcConfig;
        serverSocket = new ServerSocket(port);
    }

    @Override
    public void run() {
        while(!isStop) {
            try {
                accept();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void accept() throws IOException {
        Socket socket = serverSocket.accept();
        RpcSocketHandle socketHandle = new RpcSocketHandle(rpcConfig, socket);
        rpcConfig.getBusinessExecutorService().submit(socketHandle);
    }
}
